<?php
/**
 * Created by PhpStorm.
 * User: flydany
 * Date: 2018/2/3
 * Time: 13:04
 */
namespace common\api\wechat;

use common\api\BaseApi;

require_once "lib/WxPay.Api.php";
require_once "lib/WxPay.NativePay.php";

/**
 * 微信API
 * Wechat Recharge API
 */
class Wechat extends BaseApi {
    
    // 加密key
    public $key;
    
    public $requestSuccessCodes = [self::RequestSuccess];
    public $statusSuccessCodes = ['0'];
    
    public function __construct($config)
    {
        $this->setRequestUri($config['requestUri']);
        $this->setPartnerId($config['partnerId'])->setKey($config['key']);
    }
    
    public function setKey($key)
    {
        $this->key = $key;
        return $this;
    }
    
    /**
     * 充值申请
     * @param $params array 充值参数
     * @return array
     */
    public function recharge($params)
    {
        /**
         * 流程：
         * 1、调用统一下单，取得code_url，生成二维码
         * 2、用户扫描二维码，进行支付
         * 3、支付完成之后，微信服务器会通知支付成功
         * 4、在支付成功通知中需要查单确认是否真正支付成功（见：notify.php）
         */
        $notify = new NativePay();
        $input = new WxPayUnifiedOrder();
        $input->SetBody("test");
        $input->SetAttach("test");
        $input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
        $input->SetTotal_fee("1");
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("test");
        $input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php");
        $input->SetTrade_type("NATIVE");
        $input->SetProduct_id("123456789");
        $result = $notify->GetPayUrl($input);
        $url2 = $result["code_url"];
        $this->status = '0';
        $this->response = ['code' => SuccessCode, 'message' => 'Success', 'response' => ['javascript' => 'javascript', 'payment' => 'qrcode']];
        return $this->response;
    }
    
    /**
     * 充值查询
     * @param $params array 查询参数
     * @return array
     */
    public function rechargeQuery($params)
    {
        $this->status = '0';
        $this->response = ['code' => SuccessCode, 'message' => 'Success'];
        return $this->response;
    }
}